package com.baimifan.renovation.homefrag;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.baimifan.renovation.R;
import com.baimifan.renovation.adapter.OneClassifyAdapter;
import com.baimifan.renovation.adapter.TwoClassifyAdapter;
import com.baimifan.renovation.manager.OneClassifyManager;

import java.lang.ref.WeakReference;

/**
 * 首页-建材
 */
public class BuildFragment extends Fragment {
    private RelativeLayout build_rl;
    private Float w, h, widthPixels, heightPixels;
    private ListView listView;
    private RecyclerView recyclerView;
    private OneClassifyManager oneClassifyManager;
    private MyHandler handler;
    private OneClassifyAdapter oneClassifyAdapter;
    private TwoClassifyAdapter twoClassifyAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_build, container, false);
        init(view);
        getDisplay();
        setLayoutParams();
        oneClassifyManager.getClassify();
        return view;
    }

    private void init (View view) {
        build_rl = (RelativeLayout) view.findViewById(R.id.build_rl);
        listView = (ListView) view.findViewById(R.id.build_listview);
        recyclerView = (RecyclerView) view.findViewById(R.id.build_recycler);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        handler = new MyHandler(this);
        oneClassifyManager =new OneClassifyManager(handler);
    }

    private void getDisplay() {
        DisplayMetrics metrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
        widthPixels = Float.valueOf(metrics.widthPixels);
        heightPixels = Float.valueOf(metrics.heightPixels);
        w = widthPixels / 360;
        h = heightPixels / 640;
    }

    private void setLayoutParams () {
        LinearLayout.LayoutParams params_rl = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int)(h*44));
        build_rl.setLayoutParams(params_rl);
        LinearLayout.LayoutParams params_lv = new LinearLayout.LayoutParams((int)(w*74), ViewGroup.LayoutParams.MATCH_PARENT);
        listView.setLayoutParams(params_lv);
        LinearLayout.LayoutParams params_re = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
        recyclerView.setPadding(0, (int)(h*14), 0, 0);
        recyclerView.setLayoutParams(params_re);
    }

    private class MyHandler extends Handler {
        WeakReference<BuildFragment> mbuild;

        public MyHandler (BuildFragment buildFragment) {
            mbuild = new WeakReference<BuildFragment>(buildFragment);
        }
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            BuildFragment build = mbuild.get();
            switch (msg.what) {

                case 5:
                    oneClassifyAdapter = new OneClassifyAdapter(oneClassifyManager.getclass(), w, h);
                    listView.setAdapter(oneClassifyAdapter);
                    oneClassifyAdapter.selectPosition(0);
                    if (oneClassifyManager.getclass() != null && oneClassifyManager.getclass().size() > 0) {
                        twoClassifyAdapter = new TwoClassifyAdapter(oneClassifyManager.getclass().get(0).getList(), w, h);
                        recyclerView.setAdapter(twoClassifyAdapter);
                    }
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            if (oneClassifyManager.getclass() != null && oneClassifyManager.getclass().size() > 0) {
                                twoClassifyAdapter = new TwoClassifyAdapter(oneClassifyManager.getclass().get(position).getList(), w, h);
                                recyclerView.setAdapter(twoClassifyAdapter);
                            }
                            oneClassifyAdapter.selectPosition(position);
                            oneClassifyAdapter.notifyDataSetChanged();
                        }
                    });
                    break;
            }
        }
    }

}
